﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WCFHost.ServiceReference;

namespace WCFHost
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.AddRange(new object[] { "http://localhost:8002/FileTransfer" });
            comboBox1.SelectedIndex = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            folder.Description = "请选择文件路径";
            if (folder.ShowDialog() == DialogResult.OK)
            {
                //if (Directory.GetDirectories(folder.SelectedPath).Length > 0) //目录下存在内容
                //{
                    listView1.Items.Clear();
                    // 获取当前目录下的子目录
                    foreach (string path in Directory.GetDirectories(folder.SelectedPath))
                    {
                        ListViewItem item = new ListViewItem();
                        item.Text = path;
                        listView1.Items.Add(item);
                    }
                    // 获取当前目录下的文件
                    DirectoryInfo foderInfo = new DirectoryInfo(folder.SelectedPath);
                    foreach (FileInfo fi in foderInfo.GetFiles())
                    {
                        ListViewItem item = new ListViewItem();
                        item.Text = folder.SelectedPath + "\\" + fi.Name;
                        listView1.Items.Add(item);
                    }
                //}
            }
        }

        ServiceReference.FileTransferClient client = new ServiceReference.FileTransferClient();
        private void button3_Click(object sender, EventArgs e)
        {
            // 发送文件
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                SubPath = null;
                if (Directory.Exists(item.Text)) // 如果是目录的话
                    SubPath = Path.GetFileName(item.Text); // 获取当前目录的名称
                Send(item.Text, SubPath);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (client != null)
                client.Close();
        }

        private string SubPath = null;
        private void Send(string path, string remotingpath)
        {
            // 如果是文件，直接发送
            if (File.Exists(path))
            {
                Transfer(Path.GetDirectoryName(path), Path.GetFileName(path), remotingpath);
            }
            else if (Directory.Exists(path))
            {
                // 如果是目录, 首先发送该目录
                // 然后递归调用发送
                foreach (string dir in Directory.GetDirectories(path))
                {
                    Send(dir, remotingpath + "\\" + Path.GetFileName(dir));
                }

                foreach (string name in Directory.GetFiles(path))
                {
                    Transfer(path, Path.GetFileName(name), remotingpath);
                }
            }
            else
                return;
        }

        // 此处的path不包含文件名字
        private bool Transfer(string localpath, string name, string remotingpath)
        {
            try
            {
                // 获取文件的I/O流
                var stream = new System.IO.FileStream(Path.Combine(localpath, name), FileMode.Open);
                client.TransferFile(name, remotingpath, stream);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("传输文件失败： " + ex.ToString());
            	return false;
            }
            return true;
        }
    }
}
